package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/*
    工具类:session
 */
public class MapperUtil {
    private static SqlSessionFactory factory = null;

    private MapperUtil() {
    }

    static {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("sql.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
            //session = factory.openSession();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static SqlSession getSession() throws IOException {
        return factory.openSession();
    }

    public static void close(SqlSession session) {
        //6.事务提交
        session.commit();
        //7.释放资源
        session.close();
    }

}
